﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PC.AzureMultiApp.Core.Models;
using PC.AzureMultiApp.Storage;
using PC.AzureMultiApp.Core;
using Microsoft.WindowsAzure.ServiceRuntime;
using PC.AzureMultiApp.Core.Applications;
using PC.AzureMultiApp.WebRoleMultiApp.Helpers;

namespace PC.AzureMultiApp.WebRoleMultiApp.Controllers
{
    [ValidaAdminRole()]  
    public class CreateController : Controller
    {

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(ApplicationModel newApp)
        {
            newApp.UniqueId = Guid.NewGuid().ToString();
            newApp.ApplicationUrl = newApp.IsWebDeploy ?
                    String.Format("http://{0}/{1}", HttpContext.Request.Url.Host, newApp.ApplicationName) : string.Empty;

            Applications.Insert(newApp);

            DeploymentManager.Notify(newApp, false);

            return RedirectToAction("Application", "Application");
        }
    }
}
